package com.example.myactspringboot.util;



import java.util.Date;

/**
 * @Create 2023-09-23 14:51
 * @Description
 */
public class Time22KeyGenerator {


    /**
     * 全局循环顺序码，每次获取递增，超过999重新设置为0
     */
    private static volatile int loopCount=0;
    /**
     * 全局循环顺序码，每次获取递减，大于0重新设置为99
     */
    private static volatile int loop2Count=99;

    public Time22KeyGenerator(){
    }



    public String getNext(String type) {
        return DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS")+
                String.format("%03d",loopCount>998?loopCount=0:++loopCount)+
                String.format("%02d",loop2Count>0?--loop2Count:(loop2Count=99));
    }


    public String getNext() {
        return getNext(null);
    }



    public static void main(String[] args) {
        Time22KeyGenerator kg = new Time22KeyGenerator();
        System.out.println(kg.getNext());
//        HashSet<String> set=new HashSet<String> ();
  /*      for(int i=0;i<100;i++) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    long j=1000000000L;
                    while(j-->0) {
                        String key = kg.getNext();
						System.out.println(key);
                        if(set.contains(key)) {
                            System.err.println(key);
                        }
                    }

                }
            }).start();;

        }*/
//		int t=0;
//		for(int i=0;i<100;i++)
//			System.out.println(t>8?t=0:++t);
    }
}
